#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/6/26 17:07
# ===========================================
#       题目名称： 54. 螺旋矩阵
#       题目地址： https://leetcode.cn/problems/spiral-matrix/
#       题目描述： https://note.youdao.com/s/LWYgPSzT
# ===========================================


class Solution:
    def spiralOrder(self, matrix):
        arr = []
        while matrix:
            # 向右 x = 0
            arr.extend(matrix[0])
            del matrix[0]
            # 向下
            for index in range(len(matrix)):
                if matrix[index]:
                    val = matrix[index].pop()
                    arr.append(val)
            # 向左
            if not matrix:
                continue
            matrix[len(matrix) - 1].reverse()
            arr.extend(matrix[len(matrix) - 1])
            del matrix[len(matrix) - 1]
            # 向上
            for index in range(len(matrix) - 1, 0, -1):
                if matrix[index]:
                    val = matrix[index][0]
                    arr.append(val)
                    del matrix[index][0]
        return arr


if __name__ == '__main__':
    s = Solution()
    # [1,2,3,4,8,12,11,10,9,5,6,7]
    print("matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] =>", s.spiralOrder(matrix=[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]))
    # [1,2,3,6,9,8,7,4,5]
    print("matrix = [[1,2,3],[4,5,6],[7,8,9]] =>", s.spiralOrder(matrix=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
    # [7, 9, 6]
    print("matrix = [[7],[9],[6]] =>", s.spiralOrder(matrix=[[7], [9], [6]]))
    # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    print("matrix = [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]] =>", s.spiralOrder(matrix=[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]))
    # [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
    print("matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] =>", s.spiralOrder(matrix=[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]))
